package leecode

import (
	"fmt"
	"math"
)

func carPooling(trips [][]int, capacity int) bool {
	//求出车行驶最远的站
	toMax := math.MinInt
	for _, trip := range trips {
		toMax = max(toMax, trip[2])
	}
	//差分数组
	diff := make([]int, toMax+1)
	for _, trip := range trips {
		diff[trip[1]] = diff[trip[1]] + trip[0] // 在from站上车之后的人数
		diff[trip[2]] = diff[trip[2]] - trip[0] // 在to站离开之后的人数
	}
	fmt.Println(diff)
	var count int
	for i := 0; i < len(diff); i++ {
		count += diff[i]
		if count > capacity {
			return false
		}
	}
	return true
}
